//
//  MyMessageController.m
//  easystudy
//
//  Created by 七 on 16/9/4.
//  Copyright © 2016年 kane. All rights reserved.
//

#import "MyMessageController.h"

@interface MyMessageController ()
{
    __weak IBOutlet UILabel *titleLbl;
    __weak IBOutlet UIButton *backBtn;
    __weak IBOutlet UIView *navView;
    __weak IBOutlet UIView *BGView;
    __weak IBOutlet UILabel *lineLbl;
    __weak IBOutlet UILabel *spliteLine;
    __weak IBOutlet UIButton *myMessageBtn;
    __weak IBOutlet UIButton *myNoteBtn;
    __weak IBOutlet UILabel *messageLbl;
    __weak IBOutlet UILabel *noteLbl;
    
    __weak IBOutlet UITableView *dataTable;
    NSMutableArray *dataList;
    NSInteger dataType;
    __weak IBOutlet UILabel *tipLbl;
    
}
@end

@implementation MyMessageController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    navView.backgroundColor = KKColor_DarkGray_White;
    titleLbl.textColor = KKColor_Black_LightGary;
    
    BGView.backgroundColor = KKColor_BGColor_GrayToDarkgray;
    lineLbl.backgroundColor = KKColor_NavLineColor;
    spliteLine.backgroundColor = KKColor_NavLineColor;
    
    [myMessageBtn setImage:[UIImage imageNamed:@"my_message"] forState:UIControlStateNormal];
    [myMessageBtn setImage:[UIImage imageNamed:@"my_message_selected"] forState:UIControlStateSelected];
    
    [myNoteBtn setImage:[UIImage imageNamed:@"my_note"] forState:UIControlStateNormal];
    [myNoteBtn setImage:[UIImage imageNamed:@"my_note_selected"] forState:UIControlStateSelected];
    dataList = [NSMutableArray array];
    [self leftPressed:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)leftPressed:(id)sender {
    tipLbl.text = @"暂无消息";
    myMessageBtn.selected = YES;
    myNoteBtn.selected = NO;
    messageLbl.textColor = [UIColor colorWithHex:0xEFAD17];
    noteLbl.textColor = IS_NightMode ? [UIColor lightGrayColor]:[UIColor blackColor];
    dataType = 1;
    [self reloadDataList];
}

- (IBAction)rightPressed:(id)sender {
    tipLbl.text = @"暂无通知";
    myMessageBtn.selected = NO;
    myNoteBtn.selected = YES;
    noteLbl.textColor = [UIColor colorWithHex:0xEFAD17];
    messageLbl.textColor = IS_NightMode ? [UIColor lightGrayColor]:[UIColor blackColor];
    dataType = 2;
    [self reloadDataList];
}

- (void)reloadDataList
{
    if (dataType == 1) {
        NSDictionary *params = @{@"customer_id":[[UserEntity sharedUser] getUserID]};
        [WebService getUserMsgList:params WithBlock:^(id result, NSError *error) {
            if (!error) {
                if ([result[@"errcode"] integerValue] == 0) {
                    [dataList removeAllObjects];
                    [dataList addObjectsFromArray:result[@"result"][@"message"][@"value"]];
                    [dataTable reloadData];
                    tipLbl.hidden = dataList.count > 0;
                }
            }
        }];
    }else if (dataType == 2){
        NSDictionary *params = @{@"touser_id":[[UserEntity sharedUser] getUserID]};
        [WebService getUserMsgList:params WithBlock:^(id result, NSError *error) {
            if (!error) {
                if ([result[@"errcode"] integerValue] == 0) {
                    [dataList removeAllObjects];
                    [dataList addObjectsFromArray:result[@"result"][@"message"][@"value"]];
                    [dataTable reloadData];
                    tipLbl.hidden = dataList.count > 0;
                }
            }
        }];
    }
}

#pragma mark - User Action
- (IBAction)backPress:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)messageBtnPress:(id)sender {
}
- (IBAction)noteBtnPress:(id)sender {
    noteLbl.textColor = [UIColor colorWithHex:0xEFAD17];
    messageLbl.textColor = [UIColor blackColor];
}

#pragma mark - UITableView DataSource && Delegate

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"NewPushCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *item = dataList[indexPath.row];
    cell.textLabel.text = [item[@"log_content"] convertToString];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[item[@"create_time"] longLongValue]];
    cell.detailTextLabel.text = [date formattedDateDescription];
    return cell;
}

@end
